package com.leyou.item.web;

import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SpuService;
import com.leyou.common.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/5/4 0004
 * Time: 21:16
 */

@RestController
@RequestMapping("spu")
public class SpuController {
    @Autowired
    private SpuService spuService;

    @GetMapping("page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(@RequestParam(value = "key", required = false) String key,
                                                             @RequestParam(value = "saleable", defaultValue = "true") Boolean saleable,
                                                             @RequestParam(value = "page", defaultValue = "1") int page,
                                                             @RequestParam(value = "rows", defaultValue = "5") int rows) {

        return ResponseEntity.ok(spuService.querySpuByPage(key, saleable, page, rows));
    }

    @GetMapping("detail")
    public ResponseEntity<SpuDetailDTO> spuDetailById(@RequestParam("spuId") Long spuId) {
        return ResponseEntity.ok(spuService.spuDetailById(spuId));
    }

    @PutMapping("saleable")
    public ResponseEntity<Void> SpuSaleableById(@RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable) {
        spuService.spuSaleableById(id, saleable);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @DeleteMapping("delete")
    public ResponseEntity<Void> deleteSpuById(@RequestParam("id") Long id) {
        spuService.deleteSpuById(id);
        return ResponseEntity.status(200).build();
    }

    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(spuService.querySpuById(id));
    }


}
